package world.liuru.poem.service.impl;

import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import world.liuru.poem.entity.PoemExtendInfo;
import world.liuru.poem.repository.PoemExtendRepository;
import world.liuru.poem.service.PoemExtendService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service
public class PoemExtendServiceImpl implements PoemExtendService {
    @Resource
    PoemExtendRepository poemExtendRepository;
    @Override
    public PoemExtendInfo insertPoemExtend(PoemExtendInfo poemExtendInfo) {
        return poemExtendRepository.save(poemExtendInfo);
    }

    @Override
    public void deletePoemExtend(String id) {
        poemExtendRepository.deleteById(id);
    }

    @Override
    public PoemExtendInfo updatePoemExtend(PoemExtendInfo poemExtendInfo) {
        return poemExtendRepository.save(poemExtendInfo);
    }

    @Override
    public List<PoemExtendInfo> findAllPoemExtend() {
        return poemExtendRepository.findAll();
    }

    @Override
    public PoemExtendInfo findPoemExtendById(String id) {
        return poemExtendRepository.findById(id).orElse(null);
    }

    @Override
    public PoemExtendInfo findPoemExtendByPoemId(String id) {
        PoemExtendInfo poemExtendInfo = new PoemExtendInfo();
        poemExtendInfo.setPoemId(id);
        Example<PoemExtendInfo> example = Example.of(poemExtendInfo);
        Optional<PoemExtendInfo> one = poemExtendRepository.findOne(example);
        return one.orElse(null);
    }
}
